<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>添加文章</title>
    <link rel="stylesheet" href="/static/common/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/common/css/uploadpic.css">
    <link rel="stylesheet" href="/static/common/css/style.css">
    <script charset="utf-8" src="/static/common/kindeditor/kindeditor-all-min.js"></script>
    <script charset="utf-8" src="/static/common/kindeditor/lang/zh-CN.js"></script>
    <style type="text/css">
        .layui-upload-img {
            width: 92px;
            height: 92px;
            margin: 0 10px 10px 0;
        }
        .site-block {
            width: 80%;
            padding: 20px;
            border: 2px solid #eee;
        }
        .site-text {
            position: relative;
        }

        .title-location {
            font-size: 20px;
            font-weight: 600;
        }

        .rightlayer {
            display: inline-block;
        }

        .layui-input:focus, .layui-textarea:focus {
            border-color: red !important;
        }

        .preview-class img {
            max-width: 100%;
        }
    </style>
</head>
<body style="margin: 20px 20px ">
<div class="site-text site-block ">
    <form class="layui-form layui-form-pane " action="" lay-filter="example">
        <div class="layui-form-item">
            <label class="layui-form-label">文章标题</label>
            <div class="layui-input-block">
                <select name="article_type_id" lay-verify="required" lay-filter="county">
                    <option value="">请选择区域</option>
                <#list articleTypes as d>
                    <option value="${d.id}">${d.type}</option>
                </#list>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文章标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" id="title" required="" maxlength="30" lay-verify="required" placeholder="请输入标题名(30字内)" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文章来源</label>
            <div class="layui-input-block">
                <input type="text" name="articleFrom" id="articleFrom" required="" maxlength="30" value="www.51kuaizhi.com" lay-verify="required" placeholder="请输入标题名(30字内)" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">结束语</label>
            <div class="layui-input-block">
                <input type="text" name="theEnd" id="theEnd" required="" maxlength="50" value="" lay-verify="required" placeholder="注明责任及侵权等划分(如:本文章图片来源：新华日报，如有侵权，请联系删除)" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" pane>
            <label class="layui-form-label" style="height: 180px!important;">文章封面图</label>
            <div class="layui-input-block" style="padding:  2px 2px 2px 15px;height: 180px!important;">
                <button type="button" class="layui-btn" id="test1">上传图片</button>
                <div class="layui-input-block">
                    <img class="layui-upload-img" id="demo1" style="margin: 4px -108px;">
                    <p id="demoText" style="margin: 4px -108px;"></p>
                </div>
                <input type="hidden" name="cover_img_path" value="" id="cover_img_path">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">SEO收录词</label>
            <div class="layui-input-block">
                <input type="text" name="keywords" id="keywords" required="" maxlength="30" value="" lay-verify="required" placeholder="请输入SEO收录词" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">SEO描述</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入SEO描述(最多300字)" name="description" class="layui-textarea" maxlength="300" lay-verify="required"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">正文内容
                <button type="button" style="float: right" class="layui-btn layui-btn-primary layui-btn-xs" onclick="preview()">预览</button>
            </label>
            <div class="layui-input-block">
                <textarea id="editor_id" name="content" style="width:100%;height:700px;">${articles.content}</textarea>
            </div>
        </div>
        <input name="article_id" id="article_id" value="" type="hidden">
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="formDemo">立即更新</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
</body>
<script type="text/javascript" src="/static/common/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/static/common/layui/layui.js"></script>
<script type="text/javascript">
    var editor;
    KindEditor.ready(function (K) {
        editor = K.create('#editor_id', {
            resizeType: 2,
            uploadJson: '/util/article/uploaderImg.do' // 相对于当前页面的路径
        });
    });

    function preview() {
        var html = editor.html();
        html = '<div class="preview-class">' + html + '</div>'
        layer.open({
            type: 1,
            area: ['1024px', '800px'], //宽高
            content: html
        });
    }
</script>
<script type="text/javascript">
    layui.use('form', function () {
        var form = layui.form;
        form.on('submit(formDemo)', function (data) {
            if (editor.html() != '' && editor.html().length > 50) {
                data.field.content = editor.html();
                $.post('/article/details/updataDetails.do',data.field,function (res) {
                    if(res.data==true){
                        layer.confirm('更新成功!是否要去文章列表?', {icon: 1, title: '提示'}, function (index) {
                            window.location.href='/article/details/list.do';
                        });
                    }else{
                        layer.alert('更新失败',{icon: 2});
                    }
                });
            } else {
                layer.msg("请正确填写正文内容!", {icon: 0});
            }
            return false;
        });

        form.val('example',${gson});
        var imgPath = $('#cover_img_path').val();
        imgPath = JSON.parse(imgPath);
        $('.layui-upload-img').attr('src', imgPath.minPath);
    });

    layui.use('upload', function () {
        var $ = layui.jquery
                , upload = layui.upload;
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/officeBuilding/building/upload.do'
            , before: function (obj) {
                layer.msg('上传中...', {icon: 16, shade: 0.01});
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.success == false) {
                    layer.closeAll('loading');
                    return layer.alert('上传失败');
                } else { //上传成功
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #01b84e;">上传成功</span>');
                    $("#cover_img_path").val(JSON.stringify(res.data));
                    layer.closeAll('loading');
                    return layer.alert('上传成功');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    })
</script>
</html>